<?php
namespace Home\Controller;
use Think\Controller;

/* 导入LaneWeChat命名空间 */
use LaneWeChat\Core\ResponsePassive;

class WeixinController extends Controller {
	
	// 构造方法，导入LaneWeChat
	public function __construct(){
		vendor('LaneWeChat.lanewechat');
		parent::__construct();
	}
	
    public function index(){
    	// 实例化Wechat类
    	$wechat = new \LaneWeChat\Core\Wechat(WECHAT_TOKEN, TRUE);
    	// 验证URL信息
    	//$wechat->checkSignature();
    	// 处理微信消息，删除XML
    	echo $this->_run( $wechat->getRequest() );
    }
    
    // 接受微信消息
    private function _run( &$request ) {
    	$msgtype = $request['msgtype'];
    	
    	// 消息类型：文本
    	if( $msgtype == 'text' ) {
    		$data = $this->_text( $request );
    	}
    	
    	return $data;
    }
    
    // 处理文本信息
    private function _text( &$request ) {
    	/*
    	 * 发送文本
    	 * ResponsePassive::text($fromusername, $tousername, '文本消息内容');
    	 * $fromusername = "谁发给你的？（用户的openId）"；在变量$request['fromusername']中
    	 * $tousername = "你的公众号Id"；在变量$require['tousername']中
    	 * */
    	$fromusername = $request['fromusername'];
    	$tousername = $request['tousername'];
    	$content = $request['content'];
    	return ResponsePassive::text($fromusername, $tousername, $content);
    }
    
}